package com.study.base.netty.nio;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @description: NioFileChannel03
 * @date: 2021/1/21 13:47
 * @author: hcm
 * @version: 1.0
 */
public class NioFileChannel03 {
    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream("1.txt");
        FileChannel fileChannel = fileInputStream.getChannel();
        FileOutputStream fileOutputStream = new FileOutputStream("2.txt");
        FileChannel fileChannel1 = fileOutputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(512);

        while (true){
            //清空buffer,重置属性
            byteBuffer.clear();
            int read = fileChannel.read(byteBuffer);
            System.out.println(read);
            if(read == -1){
                //读取完毕
                break;
            }
            //将buffer数据写入到filechannel1
            byteBuffer.flip();
            fileChannel1.write(byteBuffer);
        }
        fileInputStream.close();
        fileOutputStream.close();


    }
}

